﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Enums.InsuranceProducts;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Order;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.H5.Controllers
{
    /// <summary>
    /// 保单
    /// </summary>
    public class InsuranceOrderController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult Pay(int id)
        {
            var insuranceProductOrder = ServiceFactory.InsuranceProductOrderServiceInstance.LoadById(id);
            return View(insuranceProductOrder);
        }

        /// <summary>
        /// 填写保单信息页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult CreateInsuranceOrder(long id)
        {
            var insuranceOrder = Session["InsuranceOrderModel"] as InsuranceOrderModel;
            var sellerInsuranceProducts = ServiceFactory.SellerInsuranceProductsServiceInstance.LoadById(id);
            if (sellerInsuranceProducts == null || insuranceOrder == null)
            {
                TempData["success"] = false;
                TempData["msg"] = "请重新扫码购买！";
                return RedirectToAction("Index", "Home");
            }
            insuranceOrder.SellerInsuranceProducts = sellerInsuranceProducts;
            Session["InsuranceOrderModel"] = insuranceOrder;
            return View(insuranceOrder);
        }
        /// <summary>
        /// 保单信息确认页面
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult ConfirmInsuranceOrder()
        {
            var insuranceOrderModel = Session["InsuranceOrderModel"] as InsuranceOrderModel;
            if (insuranceOrderModel == null)
                return RedirectToAction("Index", "Home");
            return View(insuranceOrderModel);
        }
        /// <summary>
        /// 创建订单并跳转到支付页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        [HttpPost]
        public ActionResult CreateInsuranceOrder()
        {
            var insuranceOrderModel = Session["InsuranceOrderModel"] as InsuranceOrderModel;
            if (insuranceOrderModel != null)
            {
                //1.创建订单
                InsuranceProductOrderController insuranceProductOrderController = new InsuranceProductOrderController();
                return insuranceProductOrderController.CreateOrder(insuranceOrderModel.SellerInsuranceProducts, new InsuranceProductOrder
                 {
                     BuyUserIDNo = insuranceOrderModel.CertificateNO,
                     BuyCount = 1,
                     BuyUserName = insuranceOrderModel.UserName,
                     BuyUserPhone = insuranceOrderModel.UserPhone,
                     BuyUserEmail = insuranceOrderModel.Email,
                     PayType = (byte)InsuranceProductOrderPayType.WeiXinPay,
                     SellerEmployeeID = insuranceOrderModel.SellerEmployeeID,
                     SellerID = insuranceOrderModel.SellerID
                 });
            }
            return RedirectToAction("Pay");
        }

        [HttpPost]
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult NextToConfirmInsuranceOrder(InsuranceOrderModel model)
        {
            Session["InsuranceOrderModel"] = model;
            return RedirectToAction("ConfirmInsuranceOrder");
        }
    }
}